# Maintainer: Kouhei Sutou <kou@clear-code.com>

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.0.1
pkgrel=2
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://arrow.apache.org/"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-flatbuffers"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-grpc"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-re2"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-thrift"
         "${MINGW_PACKAGE_PREFIX}-uriparser"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
options=("!buildflags")
source=("apache-arrow-${pkgver}.tar.gz"::"https://www.apache.org/dyn/closer.lua?action=download\&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz"
        "apache-arrow-${pkgver}.tar.gz.asc"::"https://www.apache.org/dyn/closer.lua?action=download\&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz.asc"
        "001-aarch64.patch")
sha256sums=('c814e0670112a22c1a6ec03ab420a52ae236a9a42e9e438c3cbd37f37e658fb3'
            'SKIP'
            '826595f7d29e347bef427600b9b2eb2ef57ddb83032620fa459c0dbb968135f7')
# See generate-valid-keys.sh
validpgpkeys=('35CF82A165DDBBA29B307B7497D7E8647AE7E47B'
              '87C072B8B6405B5780D66A3D02DABFDF1679D194'
              'F2A765669021A3D3094C200B29D94E228CAAD602'
              '6D09E881160096717426C638F105883A1735623D'
              '08D3564B7C6A9CAFBFF6A66791D18FCF079F8007' # Kouhei Sutou <kou@cozmixng.org>
              'E6E4AA55F38337A6EFC7A5549F453D0CC3E4F6BA'
              '265F80AB84FE03127E14F01125BCCA5220D84079'
              'E47C810A90FE21FF448DA938755E743692EA1D85'
              'D6892EA1881BD9610330AD7B0801999AF78748E8'
              'DB3D3F10215394239119F6F845127976E1E825D4'
              'DF7BAD6652219D7502C87A11CA1AB41406F9DBAD')

cmake_build_type=Release
meson_build_type=debugoptimized

source_dir=apache-${_realname}-${pkgver}
cpp_build_dir=build-${MSYSTEM}-cpp
c_glib_build_dir=build-${MSYSTEM}-c-glib

prepare() {
  cd "${source_dir}"

  patch -p1 -i "${srcdir}/001-aarch64.patch"
}

build() {
  [[ -d ${cpp_build_dir} ]] && rm -rf ${cpp_build_dir}
  mkdir -p ${cpp_build_dir} && pushd ${cpp_build_dir}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
      ../${source_dir}/cpp \
      -DARROW_BUILD_UTILITIES=ON \
      -DARROW_COMPUTE=ON \
      -DARROW_CSV=ON \
      -DARROW_DATASET=ON \
      -DARROW_FILESYSTEM=ON \
      -DARROW_FLIGHT=ON \
      -DARROW_FLIGHT_SQL=ON \
      -DARROW_GANDIVA=ON \
      -DARROW_HDFS=ON \
      -DARROW_JSON=ON \
      -DARROW_PACKAGE_KIND=${MINGW_PACKAGE_PREFIX} \
      -DARROW_PACKAGE_PREFIX=${MINGW_PREFIX} \
      -DARROW_PARQUET=ON \
      -DARROW_S3=ON \
      -DARROW_USE_GLOG=OFF \
      -DARROW_WITH_BROTLI=ON \
      -DARROW_WITH_BZ2=ON \
      -DARROW_WITH_LZ4=ON \
      -DARROW_WITH_SNAPPY=ON \
      -DARROW_WITH_ZLIB=ON \
      -DARROW_WITH_ZSTD=ON \
      -DBOOST_ROOT=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${cmake_build_type} \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_UNITY_BUILD=ON \
      -DPARQUET_BUILD_EXECUTABLES=ON \
      -DPARQUET_REQUIRE_ENCRYPTION=ON \
      -G Ninja
  ${MINGW_PREFIX}/bin/cmake --build .
  popd

  [[ -d ${c_glib_build_dir} ]] && rm -rf ${c_glib_build_dir}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    ${c_glib_build_dir} \
    ${source_dir}/c_glib \
    --buildtype=${meson_build_type} \
    --prefix=${MINGW_PREFIX} \
    -Darrow_cpp_build_dir=$(pwd)/${cpp_build_dir} \
    -Darrow_cpp_build_type=${cmake_build_type} \
    -Dgtk_doc=true
  sed -i'' -s 's/\r//g' ${c_glib_build_dir}/arrow-glib/version.h
  PATH=$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
    ${MINGW_PREFIX}/bin/meson compile -C ${c_glib_build_dir}
}

check() {
  # TODO
  # make -C ${cpp_build_dir} test

  # PATH=$(pwd)/${c_glib_build_dir}/arrow-glib:$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
  #   ninja -C ${c_glib_build_dir} test

  :
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install ${cpp_build_dir}

  PATH=$(pwd)/${c_glib_build_dir}/arrow-glib:$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install -C ${c_glib_build_dir}

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  for cmfile in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/arrow/*.cmake; do
    sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${cmfile}"
  done
}
